package BIOtest06;

import java.io.*;
import java.net.Socket;
import java.util.concurrent.ExecutionException;

public class ServerSocketThread extends Thread{
    private Socket socket;
    public ServerSocketThread(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try{
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String msg;
            while((msg=bufferedReader.readLine())!=null){
                sendmsgtosocket(msg);
            }
        }catch (Exception e){
            System.out.println("有人下线了");
        }
    }

    private void sendmsgtosocket(String msg) {
        try {

            for (Socket socket : Server.socketList) {
                PrintStream printstream = new PrintStream(socket.getOutputStream());
                printstream.println(msg);
                printstream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
